package com.dong.pointviewpager.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView

/**
 * Created by donghuadong on 2018/4/22.
 */
class RoundImageView @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0): AppCompatImageView(context, attrs, defStyleAttr) {
    private var radius = 0f
    private var radii: FloatArray? = null

    fun setRadius(radius: Float) {
        this.radius = radius
    }

    /**
     * 画图
     * @param canvas
     */
    override fun onDraw(canvas: Canvas) {
        //圆角的半径，依次为左上角xy半径，右上角，右下角，左下角
        radii = floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius)
        val path = Path()
        val width = this.width
        val height = this.height
        val rectF = RectF(0f, 0f, width.toFloat(), height.toFloat())
        radii?.run { path.addRoundRect(rectF, this, Path.Direction.CCW) }
        canvas.clipPath(path)
        super.onDraw(canvas)
    }
}